package com.ljk.study5ormmybatis.controller;

import com.ljk.study5ormmybatis.entity.User;
import com.ljk.study5ormmybatis.exception.DataException;
import com.ljk.study5ormmybatis.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * desc:
 *
 * @author ljk
 * @date 2021/3/27 17:57
 */
@RestController
@Slf4j
public class HelloController {

    private UserService userService;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping("/h")
    public Map sayHello() {
        Map<String, Object> map = new HashMap<>(10);
        List<User> allUser = userService.findAllUser();
        map.put("data", allUser.toString());
        return map;
    }

    @RequestMapping("/t")
    public Map testTransactional() {
        Map<String, Object> map = new HashMap<>(10);
        try {
            String res = userService.testTransactional();
            map.put("data", res);
        } catch (DataException e) {
            log.error(e.getMessage());
            map.put("data",e.getMessage());
        } finally {
            map.put("message", "请求成功");
        }
        return map;
    }
}
